<?php
/**
 * --------------------------------------------------------------
 * 友情链接控制器
 * --------------------------------------------------------------
 * Light Year Doc
 * @author yinq<3331653644@qq.com>
 * --------------------------------------------------------------
 */
namespace app\admin\controller;

use app\common\controller\AdminBase;
use app\admin\model\FriendLinks as LinksModel;

class FriendLinks extends AdminBase
{
    /**
     * 友情链接列表
     */
    public function index()
    {
        $map  = $this->getSimpleSearchMap();
        $list = LinksModel::where($map['where'])
                ->order('id desc')
                ->paginate(config('app.site_setting.list_rows'), false, [
                    'query' => $map['conditions']
                ]);

        $this->assign([
            'list'         => $list,
            'keyword'      => $map['conditions']['keyword'],
            'search_field' => $map['conditions']['search_field'],
            'title'        => '友情链接列表'
        ]);
        return $this->fetch();
    }
    
	/**
	 * 新增
	 */
	public function add()
	{
	    if ($this->request->isPost()) {
		    // 表单数据
            $data = $this->request->post();
            if (is_array($data['fl_time'])) {
                $data['start_time'] = isset($data['fl_time'][0]) ? $data['fl_time'][0] : '';
                $data['end_time']   = isset($data['fl_time'][1]) ? $data['fl_time'][1] : '';
            }

            // 验证
            $result = $this->validate($data, 'FriendLinks');
            if (true !== $result) $this->error($result);
			
            unset($data['fl_time']);
			
			if (LinksModel::create($data)) {
                $this->success('新增成功', 'index');
            } else {
                $this->error('新增失败');
            }
		} else {
            $this->assign([
			    'title' => '新增友情链接',
			]);
		    return $this->fetch();
		}
	}
	
	/**
	 * 编辑
	 */
	public function edit()
	{
		$id = input('id/d');
    	if ($id <= 0 || !is_numeric($id)) $this->error('参数错误');
	    if ($this->request->isPost()) {
		    // 表单数据
            $data = $this->request->post();
            if (is_array($data['fl_time'])) {
                $data['start_time'] = isset($data['fl_time'][0]) ? $data['fl_time'][0] : '';
                $data['end_time']   = isset($data['fl_time'][1]) ? $data['fl_time'][1] : '';
            }

            // 验证
            $result = $this->validate($data, 'FriendLinks');
            if (true !== $result) $this->error($result);
            
            unset($data['fl_time']);

            if (LinksModel::update($data)) {
                $this->success('编辑成功', 'index');
            } else {
                $this->error('编辑失败');
            }
		} else {
            $info = LinksModel::get($id);
            $this->assign([
			    'title' => '编辑友情链接',
				'info'  => $info
			]);
			
		    return $this->fetch();
		}
    }
    
	/**
	 * 删除
	 * @param integer $ids id值
	 */
	public function del($ids = 0)
	{
        (int) $ids || $this->error('参数错误');
        $map = ['id' => $ids];
		
        $result = LinksModel::where($map)->delete();
        return $result ? $this->success('删除成功!') : $this->error('删除失败!');
	}
	
	/**
	 * 禁用/启用
	 * @param array $ids id数组
     * @param integer $status 状态值
     */
    public function setStatus($ids = [], $status)
    {
        empty($ids) && $this->error('请选择要操作的数据');
        !is_numeric((int) $status) && $this->error('参数错误');
        
        $map  = ['id' => $ids];
        $info = LinksModel::where($map)->setField('status', $status);
        $tips = $status == 1 ? '启用' : '禁用';
        return $info !== FALSE ? $this->success($tips.'成功') : $this->error($tips.'失败');
    }
}